﻿using Newtonsoft.Json;
using System;

namespace Dotnet.Utils.Utility.Serialize
{
    public static class JsonHelper
    {
        public static string SerializeWithType(object obj)
        {
            string result;
            if (obj == null)
            {
                result = string.Empty;
            }
            else
            {
                result = SerializeWithType(obj, obj.GetType());
            }
            return result;
        }

        public static string SerializeWithType(object obj, Type type)
        {
            string result;
            if (obj == null)
            {
                result = string.Empty;
            }
            else
            {
                string arg = JsonConvert.SerializeObject(obj);
                result = string.Format("{0}{1}{2}", type.AssemblyQualifiedName, "|", arg);
            }
            return result;
        }

        public static T DeserializeWithType<T>(string serializedObj)
        {
            return (T)DeserializeWithType(serializedObj);
        }

        public static object DeserializeWithType(string serializedObj)
        {
            Validate(serializedObj);
            object result;
            if (string.IsNullOrWhiteSpace(serializedObj))
            {
                result = null;
            }
            else
            {
                int num = serializedObj.IndexOf("|", StringComparison.InvariantCultureIgnoreCase);
                Type type = Type.GetType(serializedObj.Substring(0, num));
                string text = serializedObj.Substring(num + 1);
                result = JsonConvert.DeserializeObject(text, type);
            }
            return result;
        }

        public static T Deserialize<T>(string json)
        {
            Validate(json);
            T result;
            try
            {
                result = JsonConvert.DeserializeObject<T>(json);
            }
            catch (Exception innerException)
            {
                throw new Exception(json, innerException);
            }
            return result;
        }

        public static void Validate(string str)
        {
            if (str.Contains("$type") && !str.Contains("\\\"$type\\\":\\\"InsCES3."))
            {
                throw new Exception("非法的Json：" + str);
            }
        }

        public static T Deserialize<T>(string json, JsonSerializerSettings settings)
        {
            Validate(json);
            T result;
            try
            {
                result = JsonConvert.DeserializeObject<T>(json, settings);
            }
            catch (Exception innerException)
            {
                throw new Exception(json, innerException);
            }
            return result;
        }

        public static object Deserialize(Type type, string json)
        {
            Validate(json);
            object result;
            try
            {
                result = JsonConvert.DeserializeObject(json, type);
            }
            catch (Exception innerException)
            {
                throw new Exception(json, innerException);
            }
            return result;
        }

        public static string Serialize(object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }

        public static string Serialize(object obj, JsonSerializerSettings settings)
        {
            return JsonConvert.SerializeObject(obj, settings);
        }

        private const string TypeSeperator = "|";
    }
}